package com.fangs.yehwangglobal.ui.fragment

import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.Menu
import android.view.MenuInflater
import android.view.ViewGroup
import com.fangs.yehwangglobal.R
import com.fangs.yehwangglobal.base.BaseFragment
import com.fangs.yehwangglobal.base.BaseResultEntity
import com.fangs.yehwangglobal.databinding.FragmentHomeBinding
import com.fangs.yehwangglobal.model.ArticleListBean
import com.fangs.yehwangglobal.net.BaseObserver
import com.fangs.yehwangglobal.net.NetworkTools
import com.gyf.immersionbar.ImmersionBar
import android.view.View

import android.widget.TextView
import androidx.annotation.Nullable

import androidx.recyclerview.widget.RecyclerView

import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders


class HomeFragment : BaseFragment<FragmentHomeBinding>() {

    var LL_SEARCH_MIN_TOP_MARGIN: Float = 0.0f
    var LL_SEARCH_MAX_TOP_MARGIN: Float = 0.0f
    var LL_SEARCH_MAX_WIDTH: Float = 0.0f
    var LL_SEARCH_MIN_WIDTH: Float = 0.0f
    var TV_TITLE_MAX_TOP_MARGIN: Float = 0.0f
    lateinit var searchLayoutParams: ViewGroup.MarginLayoutParams
    lateinit var titleLayoutParams: ViewGroup.MarginLayoutParams

    companion object {
        fun getInstance() = HomeFragment()
    }

    override fun afterCreate(savedInstanceState: Bundle?) {

        val  homeViewModel = ViewModelProviders.of(this).get(HomeViewModel::class.java)
        homeViewModel.text.observe(viewLifecycleOwner, object : Observer<String?> {
            override fun onChanged(@Nullable s: String?) {}
        })

        //状态栏字体是深色，不写默认为亮色
        ImmersionBar.with(this)
            .statusBarDarkFont(true) //状态栏字体是深色，不写默认为亮色
            .fitsSystemWindows(true)
            .statusBarColor(R.color.on_boarding) //状态栏颜色，不写默认透明色
            .autoDarkModeEnable(true)//自动状态栏字体和导航栏图标变色，必须指定状态栏颜色和导航栏颜色才可以自动变色
            .init()

//        initRefreshLayout()

    }

    val pager = 1
    private fun initRefreshLayout() {
        NetworkTools.request(activity, true,
            NetworkTools.api.getArticleList(pager),
            object : BaseObserver<BaseResultEntity<ArticleListBean?>?>() {
                override fun onSuccess(t: BaseResultEntity<ArticleListBean?>?) {
                    if (!t!!.isOk()) {
                        Log.e("--data--", t.data?.datas.toString())
                        return
                    }
                }
                override fun onFail(t: Throwable?) {
                    Log.e("--data--", t.toString())
                }
            })
    }

    override fun lazyLoadData() {
        /*searchLayoutParams =
            viewBinding.searchLlSearch.getLayoutParams() as ViewGroup.MarginLayoutParams
        titleLayoutParams =
            viewBinding.searchTvTitle.getLayoutParams() as ViewGroup.MarginLayoutParams

        LL_SEARCH_MIN_TOP_MARGIN =
            DisplayUtils.dip2px(requireActivity(), 4.5f).toFloat() //布局关闭时顶部距离

        LL_SEARCH_MAX_TOP_MARGIN =
            DisplayUtils.dip2px(requireActivity(), 49f).toFloat() //布局默认展开时顶部距离

        LL_SEARCH_MAX_WIDTH =
            (DisplayUtils.getScreenWidth(requireActivity()) - DisplayUtils.dip2px(
                requireActivity(),
                30f
            )).toFloat() //布局默认展开时的宽度

        LL_SEARCH_MIN_WIDTH =
            (DisplayUtils.getScreenWidth(requireActivity()) - DisplayUtils.dip2px(
                requireActivity(),
                82f
            )).toFloat() //布局关闭时的宽度

        TV_TITLE_MAX_TOP_MARGIN = DisplayUtils.dip2px(requireActivity(), 11.5f).toFloat()

        viewBinding.searchSvView.setOnAnimationScrollListener(object :
            OnAnimationScrollChangeListener {
            override fun onScrollChanged(dy: Float) {
                var searchLayoutNewTopMargin: Float = LL_SEARCH_MAX_TOP_MARGIN - dy
                var searchLayoutNewWidth: Float =
                    LL_SEARCH_MAX_WIDTH - dy * 1.3f //此处 * 1.3f 可以设置搜索框宽度缩放的速率
                val titleNewTopMargin = (TV_TITLE_MAX_TOP_MARGIN - dy * 0.5)

                //处理布局的边界问题
                searchLayoutNewWidth =
                    if (searchLayoutNewWidth < LL_SEARCH_MIN_WIDTH) LL_SEARCH_MIN_WIDTH else searchLayoutNewWidth
                if (searchLayoutNewTopMargin < LL_SEARCH_MIN_TOP_MARGIN) {
                    searchLayoutNewTopMargin = LL_SEARCH_MIN_TOP_MARGIN
                }
                if (searchLayoutNewWidth < LL_SEARCH_MIN_WIDTH) {
                    searchLayoutNewWidth = LL_SEARCH_MIN_WIDTH
                }
                var titleAlpha: Float =
                    (255 * titleNewTopMargin / TV_TITLE_MAX_TOP_MARGIN).toFloat()
                if (titleAlpha < 0) {
                    titleAlpha = 0f
                }

                //设置相关控件的LayoutParams  此处使用的是MarginLayoutParams，便于设置params的topMargin属性
                viewBinding.searchTvTitle.setTextColor(
                    viewBinding.searchTvTitle.getTextColors().withAlpha(titleAlpha.toInt())
                )
                titleLayoutParams.topMargin = titleNewTopMargin.toInt()
                viewBinding.searchTvTitle.setLayoutParams(titleLayoutParams)
                searchLayoutParams.topMargin = searchLayoutNewTopMargin.toInt()
                searchLayoutParams.width = searchLayoutNewWidth.toInt()
                viewBinding.searchLlSearch.setLayoutParams(searchLayoutParams)
            }
        })*/


        (activity as AppCompatActivity?)!!.setSupportActionBar(viewBinding.toolbar)
        setHasOptionsMenu(true)

        viewBinding.recyclerView.setAdapter(object : RecyclerView.Adapter<RecyclerView.ViewHolder?>() {
            override fun onCreateViewHolder(
                parent: ViewGroup,
                viewType: Int
            ): RecyclerView.ViewHolder {
                return ViewHolder(layoutInflater.inflate(R.layout.item_simple, parent, false))
            }

            override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
                val vh = holder as ViewHolder?
                vh!!.text.text = "Fake Item " + (position + 1)
                vh!!.text2.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
            }

            override fun getItemCount(): Int {
                return 20
            }

            inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
                var text: TextView
                var text2: TextView

                init {
                    text = itemView.findViewById(R.id.text)
                    text2 = itemView.findViewById(R.id.text2)
                }
            }
        })
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater.inflate(R.menu.menu_main, menu)
        super.onCreateOptionsMenu(menu, inflater)
    }
}